package com.he.DesignPatterns.observer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 具体主题类
 * @Author: HCL
 * @Date: 2024/9/4 10:34
 */
public class SubscriptionSubject implements Subject {

    //添加存储观察者对象的集合
    private List<Observer> observerList = new ArrayList<>();

    @Override
    public void attach(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void detach(String name) {
        Iterator<Observer> iterator = observerList.iterator();
        while (iterator.hasNext()) {
            WeiXinUser weiXinUser = (WeiXinUser) iterator.next();
            if (weiXinUser.getName().equalsIgnoreCase(name)) {
                iterator.remove();
            }
        }
    }

    @Override
    public void notify(String message) {
        observerList.forEach(o -> o.update(message));
    }
}
